package com.javasm.adminapi.module.pricecheck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.adminapi.common.RedisKeys;
import com.javasm.adminapi.common.exception.ExceptionEnum;
import com.javasm.adminapi.common.exception.JavasmException;
import com.javasm.adminapi.module.priceadjust.entity.GgPriceAdjust;
import com.javasm.adminapi.module.priceadjust.service.GgPriceAdjustService;
import com.javasm.adminapi.module.pricecheck.dao.GgAdjustCheckDao;
import com.javasm.adminapi.module.pricecheck.entity.GgAdjustCheck;
import com.javasm.adminapi.module.pricecheck.service.GgAdjustCheckService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (GgAdjustCheck)表服务实现类
 *
 * @author makejava
 * @since 2025-10-18 09:43:21
 */
@Service("ggAdjustCheckService")
public class GgAdjustCheckServiceImpl extends ServiceImpl<GgAdjustCheckDao, GgAdjustCheck> implements GgAdjustCheckService {

    @Resource
    GgPriceAdjustService priceAdjustService;


    @Resource
    GgAdjustCheckDao adjustCheckDao;

    @Resource
    RedisTemplate<String,GgAdjustCheck> redisTemplate;

    @Transactional
    @Override
    public void handleCheck(GgAdjustCheck check) {
        //先选择是否有传申请的id
        Integer adjustId = check.getAdjustId();

        if (adjustId==null){
            throw new JavasmException(ExceptionEnum.Report_null);
        }
        //有id，重新封装对象赋值（带审核记录）
        //先save
        //TODO从SpringSecurity框架中获取到登陆人信息set到check的name中
        save(check);
        check= adjustCheckDao.getAdjustReport(adjustId);
        Integer checkAnswer = check.getCheckAnswer();
        GgPriceAdjust ggPriceAdjust = check.getGgPriceAdjust();
        if (ggPriceAdjust==null){
            throw new JavasmException(ExceptionEnum.Parameter_Null);
        }
        if (checkAnswer==0){
            //未通过
            //设置状态审核为0
            //修改时间
            ggPriceAdjust.setUpdateTime(new Date());
            ggPriceAdjust.setProcessStatus("0");
            //根据id修改申请的状态
            priceAdjustService.updateById(ggPriceAdjust);
        }else if (checkAnswer==1){
            ggPriceAdjust.setUpdateTime(new Date());
            ggPriceAdjust.setProcessStatus("1");
            //根据id修改申请的状态
            priceAdjustService.updateById(ggPriceAdjust);
        }
        check.setCheckTime(new Date());
        //更新
        updateById(check);
    }

    @Override
    public GgAdjustCheck getByAdjustId(Integer adjustId) {
        //高频，且永久的，直接存到redis中，不在连接数据库
        String key = String.format(RedisKeys.CheckReport,adjustId);
        GgAdjustCheck check = redisTemplate.opsForValue().get(key);
        if (check==null){
            LambdaQueryWrapper<GgAdjustCheck> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(GgAdjustCheck::getAdjustId,adjustId);
            check= getOne(queryWrapper);
            //因为记录是永久的，所以不用设置时间限制
            redisTemplate.opsForValue().set(key,check);
        }

        return check;
    }
}

